#ifndef ASPRITE_INSTANCE_H
#define ASPRITE_INSTANCE_H

#include "ASprite.h"
#include <rect.h>

class ASpriteInstance
{
	public:	
		ASpriteInstance(ASprite* pCurrentSprite);
		~ASpriteInstance();

		void Stop();
		void SetAnim(int anim, int loop = 0xFFFFFF);
		void SetPosition(int x, int y);
		void SetASprite(ASprite* sprite);

		ASprite* GetASprite () const { return m_pCurrentSprite; }

		int GetCurrentAnim() { return m_nCurrentAnim; }
		int GetCurrentLoop() { return m_nLoop; }
        irr::core::rect<int> GetAnimRect();

		void StartAnim(ASprite* sprite, int anim, int x, int y, int loop = 0xFFFFFF);
		void SetTransitionAnim(int anim, int nextAnim);
		void SetAchievable(bool achievable);

		bool IsAnimOver() const;
		bool IsAnimOver2() const;

		void Update();
		void Draw(EXT_DRAW_INFO* info = null) const;
		void Draw3D(irr::core::vector3df pos = vector3df(), irr::f32 xscale = 4.f, irr::f32 yscale = 4.f, int flags = 0,bool isFaceCamera = true);

		void SetScale (float x, float y) { m_scaleX = x; m_scaleY = y; }
        void Set2DScale(int arg_2DScale_x, int arg_2DScale_y) {m_2DScale_X = arg_2DScale_x; m_2DScale_Y = arg_2DScale_y;}
		float GetScaleX () const { return m_scaleX; }
		float GetScaleY () const { return m_scaleY; }

		void SetFlippedX (bool value) { m_flippedX = value; }
		bool GetFlippedX () const { return m_flippedX; }

		int GetCurrentAnimTime() const{ return m_nCurrentAnimTime; }
		int GetCurrentAnimTimeAll();

		bool GetIsAnimFinished() const { return m_bAnimFinished;}

		//////////////////////////////////////////////////////////////////////////
		bool		IsCurAnimFinished() {return m_bCurAnimFinished;}
		bool		IsPlaying() const { return m_bIsPlaying;}
		void		Play(const vector3df &Pos, bool loop = false)
		{
			m_bIsPlaying = true;
			m_3DPos = Pos;
			m_nLoop = loop ? 0xFFFFFF : 0;
		}

		void	SetScaleAnim(float	scale) { m_fScaleAnim = scale;};
public:
		int m_nCurrentAFrame;
protected:
		int m_nX;
		int m_nY;

		int m_nCurrentAnim;
		
		int m_nCurrentFTime;
		int m_nCurrentAnimTime;
		int m_nCurrentAnimTimeAll;
		int m_nLoop;

		bool m_bAchievable;
		bool m_bAnimFinished;

		bool m_flippedX;

		float m_scaleX;
		float m_scaleY;

        int m_2DScale_X;
        int m_2DScale_Y;

		int m_nNextAnim;
		
		float m_fScaleAnim;
		////////////////////////////////////////////////////////////////////////////////
public:
		bool		m_bIsPlaying;
		bool		m_bCurAnimFinished;
		vector3df	m_3DPos;
		ASprite* m_pCurrentSprite;
};

template <class T> inline
void Maximize( irr::core::rect<T>& rectMax, const irr::core::rect<T>& rectOther )
{
    if ( rectMax.UpperLeftCorner.X > rectOther.UpperLeftCorner.X )
        rectMax.UpperLeftCorner.X = rectOther.UpperLeftCorner.X;
    if ( rectMax.UpperLeftCorner.Y > rectOther.UpperLeftCorner.Y )
        rectMax.UpperLeftCorner.Y = rectOther.UpperLeftCorner.Y;

    if ( rectMax.LowerRightCorner.X < rectOther.LowerRightCorner.X )
        rectMax.LowerRightCorner.X = rectOther.LowerRightCorner.X;
    if ( rectMax.LowerRightCorner.Y < rectOther.LowerRightCorner.Y )
        rectMax.LowerRightCorner.Y = rectOther.LowerRightCorner.Y;
}


#endif //ASPRITE_INSTANCE_H